/**
* @description : 卷烟品牌查询
* @descriptionDetail : 卷烟品牌查询
* @copyright : 浙江烟草
* @author : mc
* @create : 2022-12-05 15:33:20
*/
<template>
  <view>
    <!-- #ifdef H5-ONLY -->
    <theme-navbar
      :is-white="false" :title="navTitle" :custom-back="goBack"
      style="z-index: 99999"
    >
      <view class="nav-right-icon">
        <u-icon name="scan" color="#FFF" @tap="scan"></u-icon>
      </view>
    </theme-navbar>
    <!-- #endif -->
    <!-- #ifdef H5-WECHAT || H5-EMP -->
    <fab-button :content="fContent" @fabtrigger="fabtrigger" />
    <!-- #endif -->
    <view class="content u-page">
      <!-- 内容区 -->
      <view class="nolicence-search-lay" :style="{'top': isHideNav ? 0 : '44px'}">
        <u-search
          class="nolicence-search" shape="round" :clearabled="true"
          :showAction="true" actionText="确定" :animation="true"
          placeholder="请输入品牌名称/烟码" v-model="keyword" @custom="searchClick"
        ></u-search>
      </view>
      <view class="main">
        <me-table
          :i="0" :index="0" :top="80"
          :initLoad="false" :load-data="loadData" :height="getListViewHeight()"
          ref="mescrollItem0"
        >
          <view slot-scope="d">
            <logo-info :cigar="d.data" class="smoke_info_item"></logo-info>
          </view>
        </me-table>
      </view>
    </view>
  </view>
</template>

<script>
import meTable from '@/components/me-table.vue';
import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
import LogoInfo from "./components/logoInfo";
import zjCommonService from "@/service/common/common.service";
import commonService from "@/service/case/case.service.js";
import fabButton from '@/components/uni-fab.vue';

export default {
  components: {
    meTable,
    LogoInfo,
    fabButton,
  },
  mixins: [MescrollMoreMixin,],
  data() {
    return {
      // 搜索框关键字
      keyword: "",
      fContent: [{
        iconPath: require('@/static/scode.png'),
        selectedIconPath: require('@/static/scode.png'),
        text: '扫一扫',
        active: false,
      },],
      isHaveLic: 1,
      isLicNoCode: false,
      licNoCache: '',
      cigCode: '',
    };
  },
  mounted() {
    this.setRightBtns();
    this.init();
  },
  onLoad() {
    console.log('77777')
    document.addEventListener("keydown", this.keypress);
  },
  onUnload() {
    console.log('onUnload');
  },
  methods: {
    /**
			 * 初始化
			 */
    init() {},
    setRightBtns() {
      // 设置右侧标题
      // #ifdef H5-DINGDING
      console.log("设置标题", this.tabIndex);
      this.$deviceApi.setRight("扫一扫", () => {
        // 添加日计划操作
        this.scan();
        this.setRightBtns();
      }, true, true);
      // #endif
    },
    fabtrigger(btn) {
      if (btn.index == 0) {
        this.scan();
      }
    },
    /**
			 * 筛选数据
			 */
    searchClick(val) {
      let mescroll = this.getMescroll(this.tabIndex);
      mescroll.triggerDownScroll();
      this.cigCode = ''
    },
    getListViewHeight() {
      let x = "0px";
      // #ifdef H5-DINGDING
      x = (this.$u.sys().windowHeight - 50 - this.$u.sys().statusBarHeight) + "px";
      // #endif
      // #ifndef H5-DINGDING || H5-WECHAT
      x = (this.$u.sys().windowHeight - 50 - 44 - this.$u.sys().statusBarHeight) + "px";
      // #endif
      return x;
    },
    scan() {
      // #ifdef H5-DINGDING
      this.$deviceApi.scan().then(res => {
        this.keyword = res;
        let mescroll = this.getMescroll(this.tabIndex);
        mescroll.triggerDownScroll();
      });
      // #endif
      // #ifdef H5-WECHAT || H5-EMP
      this.$deviceApi.scan().then(res => {
        this.keyword = res;
        let mescroll = this.getMescroll(this.tabIndex);
        mescroll.triggerDownScroll();
      });
      // #endif
      // #ifndef H5-DINGDING

      // this.$modal.toast("使用钉钉打开");

      // #endif
    },
    loadData(page) {
      let self = this;
      return new Promise((resolve, reject) => {
        commonService.getCigaretteList(page.num, page.size, {
          keyword: this.keyword,
        }).then(res => {
          if (res.success) {
            resolve({
              data: res.data,
              count: res.count,
            });
          } else {
            self.$modal.alert(res.msg);
            resolve({
              data: [],
            });
          }
        }).catch(err => {
          self.$modal.alert("请求失败");
          resolve({
            data: [],
          });
        });
      });
    },
    /**
			 * 跳转个人的具体任务条目内容页面
			 */
    showDetail(data) {
      const params = {
        retailerUuid: data.retailerUuid,
      };
      this.setRouteParam(params);
      this.$u.route(this.detailPath);
    },
    keypress(e) {
      console.log('keyListener:', e.keyCode)
      console.log('ele', e)
      console.log('keyValue', e.key)
      if (e.keyCode === 13) { // 扫码枪结束增加tab键标识，代码内容识别完成，一维码扫描
        console.log('keyListener13----:', e.keyCode)
        this.isLicNoCode = false;
        console.log('cigCode', this.cigCode)
        this.keyword = this.cigCode
        setTimeout(() => {
          this.searchClick()
        }, 100)
      }
      if (e.keyCode === 62) { // 空格,零售户号标识处理，二维码扫描
        this.isLicNoCode = true;
      }
      if (e.keyCode === 143 || e.keyCode === 144) {
        if (this.isLicNoCode) {
          document.removeEventListener("keydown", this.keypress);
          let data = {};
          data.value = this.licNoCache.trim().replace(/[\s\n\t]+$/g, "");
          uni.$emit('scanCallBack', {
            msg: '扫码回调',
            value: data.value,
          })
          this.licNoCache = '';
          return;
        }
      }
      if (this.isLicNoCode) { // 零售许可证标识
        this.licNoCache += this.keyValue(e.keyCode)
      } else {
        this.cigCode += e.key
      }
    },
    keyValue(keyCode) {
      switch (keyCode) {
      case 7:
        return 0;
      case 8:
        return 1;
      case 9:
        return 2;
      case 10:
        return 3;
      case 11:
        return 4;
      case 12:
        return 5;
      case 13:
        return 6;
      case 14:
        return 7;
      case 15:
        return 8;
      case 16:
        return 9;
      default:
        return '';
      }
    },
    scanCallBack() {
      // 线索登记回调事件
      uni.$on('scanCallBack', data => {
        console.log('---scanCallBack--', data)
        this.keyword = data.value
        this.searchClick()
      })
    },
  },
};
</script>

<style lang="scss" scoped>
	@import "@/style/common/rightMenu.scss";

	.nolicence-search-lay {
		background-color: #fff;
		position: fixed;
		// #ifndef H5-DINGDING
		top: 44px;
		// #endif
		// #ifdef H5-DINGDING
		top: 0px;
		// #endif
		z-index: 999;
		width: 100%;
		overflow: hidden;
	}

	.nolicence-search {
		margin: 10rpx 40rpx !important;
	}

	.main {
		background-color: $uni-bg-color-grey;
	}

	.dataTab {
		width: 730rpx;
		margin-left: 10rpx;
	}

	.name_a {
		color: blue;
	}

	.smoke_info_item {
		float: left;
		overflow: hidden;
		margin: 15px 10px 0px;
		background-color: #FFFFFF;
		border-radius: 30rpx;
	}

	.smoke_info_item:nth-child(2) {
		margin-top: 0;
	}

	.scoll_content {
		background-color: #EFF3F9;
	}
</style>
